<?php

/**
 * @version     1.0.0
 * @package     com_general
 * @copyright   Copyright (C) 2012. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Quang <quang4it@yahoo.com> - http://quang.com
 */
// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.view');
jimport('joomla.application.component.controlleradmin');

ini_set('display_errors',1);
error_reporting(E_ALL);
/**
 * View class for a list of General.
 */
class GeneralViewListallalbum extends JViewLegacy {

    protected $items;
    protected $pagination;
    protected $state;
    protected $params;

    /**
     * Display the view
     */
    public function display($tpl = null) {
        $app = JFactory::getApplication();
        $this->state = $this->get('State');
        $this->items = $this->get('Items');
        $this->pagination = $this->get('Pagination');
        $this->params = $app->getParams('com_general');
        
        $modelItems = $this->getOtherModel("items");
        
        $modelCats = $this->getOtherModel("categories", "CategoriesModel");
        $modelCats->setState('filter.extension', "com_general");
        $this->cats = $modelCats->getItems();
        foreach($this->items as $k => &$item){
            $item->imageItems = array();
            $modelItems->setState('filter.state', 1);
            $modelItems->setState('filter.album', $item->id);
            $imageItems = $modelItems->getItems();
            if(count($imageItems)){
                foreach($imageItems as $imgItem){
                    $registry = new JRegistry;
                    $registry->loadString($imgItem->images);
                    $item->imageItems[] = $registry->toArray();
                }
            }
            $registry = new JRegistry;
            $registry->loadString($item->image);
            $item->image = $registry->toArray();
            $item->image = isset($item->image['image']) && $item->image['image'] ? $item->image['image'] : "";
        }
        
        // Check for errors.
        if (count($errors = $this->get('Errors'))) {
            throw new Exception(implode("\n", $errors));
        }

        $this->_prepareDocument();

        parent::display($tpl);
    }

    /**
     * Prepares the document
     */
    protected function _prepareDocument() {
        $app = JFactory::getApplication();
        $menus = $app->getMenu();
        $title = null;

        // Because the application sets a default page title,
        // we need to get it from the menu item itself
        $menu = $menus->getActive();
        if ($menu) {
            $this->params->def('page_heading', $this->params->get('page_title', $menu->title));
        } else {
            $this->params->def('page_heading', JText::_('com_general_DEFAULT_PAGE_TITLE'));
        }
        $title = $this->params->get('page_title', '');
        if (empty($title)) {
            $title = $app->getCfg('sitename');
        } elseif ($app->getCfg('sitename_pagetitles', 0) == 1) {
            $title = JText::sprintf('JPAGETITLE', $app->getCfg('sitename'), $title);
        } elseif ($app->getCfg('sitename_pagetitles', 0) == 2) {
            $title = JText::sprintf('JPAGETITLE', $title, $app->getCfg('sitename'));
        }
        $this->document->setTitle($title);

        if ($this->params->get('menu-meta_description')) {
            $this->document->setDescription($this->params->get('menu-meta_description'));
        }

        if ($this->params->get('menu-meta_keywords')) {
            $this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
        }

        if ($this->params->get('robots')) {
            $this->document->setMetadata('robots', $this->params->get('robots'));
        }
    }

    protected function &getOtherModel($name, $prefix = 'GeneralModel') {
        $controller = new JControllerAdmin;
        $model = $controller->getModel($name, $prefix, array('ignore_request' => true));
        return $model;
    }

}
